package com.hongyuan.sellfitness.ui.main.view_mode;

import android.app.Application;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.data.md5.BaseUtil;
import com.hongyuan.sellfitness.ui.main.activity.MainActivity;
import com.hongyuan.sellfitness.ui.main.activity.SuccessActivity;
import com.hongyuan.sellfitness.ui.main.beans.AlipayBean;
import com.hongyuan.sellfitness.ui.main.beans.V3SuccessBeans;
import com.hongyuan.sellfitness.ui.person.activity.MyOrdersActivity;
import com.hongyuan.sellfitness.wxapi.WecathPayBean;

public class PayViewModel extends CustomViewModel<MyRepository> {

    public ObservableField<V3SuccessBeans> entity = new ObservableField<>();

    public ObservableField<String> payType = new ObservableField<>();

    public PayViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);

        payType.set("wechatPay");
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //支付类型ui变化
        public SingleLiveEvent<String> changPayType = new SingleLiveEvent<>();
        //去吊起微信
        public SingleLiveEvent<WecathPayBean> openWX = new SingleLiveEvent<>();
        //去吊起支付宝支付
        public SingleLiveEvent<AlipayBean> openAlipay = new SingleLiveEvent<>();
    }

    //微信支付
    public BindingCommand wechatPay = new BindingCommand(() -> {
        payType.set("wechatPay");
        uc.changPayType.setValue(payType.get());
    });

    //支付宝支付
    public BindingCommand alipayPay = new BindingCommand(() -> {
        payType.set("alipayPay");
        uc.changPayType.setValue(payType.get());
    });

    //取消支付
    public BindingCommand cancelPay = new BindingCommand(() -> {
        startActivity(MyOrdersActivity.class);
        finish();
    });

    //吊起支付
    public BindingCommand getPay = new BindingCommand(() -> {
        if("wechatPay".equals(payType.get())){
            requestData(Constants.GETPREPAYORDER);
        }else if("alipayPay".equals(payType.get())){
            requestData(Constants.UNIFIEDORDER);
        }
    });

    /*
    * 初始化显示数据
    * */
    public void setEntity(V3SuccessBeans beans){
        if(BaseUtil.isValue(beans)){
            entity.set(beans);
        }
    }

    /*
     * 三版跳转
     * */
    public void goSuccess(V3SuccessBeans successBeans){

        if(BaseUtil.isValue(successBeans)){
            Bundle bundle = new Bundle();
            bundle.putSerializable("successBeans",successBeans);
            startActivity( SuccessActivity.class,bundle);
            finish();
        }else{
            Bundle bundle = new Bundle();
            bundle.putString("titleName","支付结果");
            bundle.putString("successText","支付成功！");
            bundle.putString("buttonText","完成");
            startActivity( SuccessActivity.class,bundle);
            finish();
        }

    }

    /*
     * 跳转处理
     * */
    public void goNext(V3SuccessBeans successBeans){

        if(BaseUtil.isValue(successBeans)){
            switch (successBeans.getType()){
                case BUYCARD:
                case BUYGOODS:
                case GROUPCLASS:
                case PRIVITECLASS:
                    startActivity(MyOrdersActivity.class);
                    break;
                default:
                    startActivity(MainActivity.class);
                    break;
            }
        }else{
            startActivity(MainActivity.class);
        }
        finish();
    }


    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GETPREPAYORDER){
            WecathPayBean payBean = (WecathPayBean)dataBean;
            uc.openWX.setValue(payBean);
        }

        if(code == Constants.UNIFIEDORDER){
            AlipayBean paybean = (AlipayBean)dataBean;
            uc.openAlipay.setValue(paybean);
        }
    }
}
